﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tournament
{
    public class Tourney
    {

        List<double> judgepoints = new List<double>(); //Lista för att stoppa in judge points.

        public double SelectJumpingHeight(double Height) //Test funktion
        {
            if (Height == 1 || Height == 3 || Height == 5 || Height == 7.5 || Height == 10)
                return Height;
            else
                Console.WriteLine("ERROR, You can only choose from height 1, 3, 5, 7.5 or 10");
            return 0;
        }

        public string SelectGender(string gender) //Test funktion
        {
            string Error = "ERROR: No gender selected";
            if (gender == "male" || gender == "Male")
                return gender;
            else if (gender == "female" || gender == "Female")
                return gender;
            else
                return Error;
        }

        public int MaxDivers(int divers)
        {
            return divers;
        }

        public int AmountOfJudges(int judges)
        {
            return judges;
        }

        public double CalcPts(double first, double second, double third, double fourth, double fifth) //Räknar ut antal poäng som ges av Dommmare och tar bort största/minsta värde
        {
            double totalpts = 0;
            double max = 0;
            double min;
            int i = 0;

            judgepoints.Add(first);
            judgepoints.Add(second);
            judgepoints.Add(third);
            judgepoints.Add(fourth);
            judgepoints.Add(fifth);

            judgepoints.Sort();

            max = judgepoints.Max();
            min = judgepoints.Min();


            for (i = 0; i < judgepoints.Count(); i++)
            {
                if (judgepoints[i] == max)
                {
                    judgepoints.RemoveAt(i);
                    max = -1;
                }
            }

            for (i = 0; i < judgepoints.Count(); i++)
            {           
                if (judgepoints[i] == min)
                {
                    judgepoints.RemoveAt(i);
                    min = -1;
                }
            }

            

            foreach (var points in judgepoints)
                totalpts += points;



            return totalpts; //CalcPts() * DD = Diverspoints
        }



        public struct Dive //Structen för Divelistan
        {
            public string DiveId;
            public string JumpHeight;
            public string DiveName;
            public string DD;
        };


        public List<Dive> DiveList = new List<Dive>();

        public void Readfromfile() // Läser in alla hoppkoder till en lista
        {
            string line = "";
            Dive list = new Dive();

            

            using (StreamReader sr = new StreamReader("JumpList.txt"))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    string[] split = line.Split(';');
                    list.DiveId = split[0];
                    list.JumpHeight = split[1];
                    list.DiveName = split[2];
                    list.DD = split[3];
                    DiveList.Add(list);
                }
                sr.Close();
            }
        }

        public string Search(string height) //Kollar om hoppet finns i listan
        {

            bool IsFound = false;
            string divecode = "";

            
            Console.Write("Input the divecode for the dive you want to execute (EXAMPLE: 5321D) : ");
            divecode = Console.ReadLine();
            Console.Write("\n");
            int i;

            for (i = 0; i < DiveList.Count(); i++)
            {
                if (divecode == DiveList[i].DiveId && height == DiveList[i].JumpHeight)
                {
                    Console.WriteLine(DiveList[i].DiveId + "     " + DiveList[i].JumpHeight + "     " + DiveList[i].DiveName + "     " + DiveList[i].DD + "\n");
                    IsFound = true;
                    break;
                }
            }
            if (IsFound == true)
            {
                return DiveList[i].DiveId;
            }
            else
            {
                Console.WriteLine("Not Found");
                return "";
            }


        }

        public double CalcPtsnDD(string divecode1 , string height1, double first, double second, double third, double fourth, double fifth)
        {
            int i;
            double DDval = 0;
            double finalscore = 0;
            for (i = 0; i < DiveList.Count(); i++)
            {
                if (divecode1 == DiveList[i].DiveId && height1 == DiveList[i].JumpHeight)
                {
                    DDval = double.Parse(DiveList[i].DD, CultureInfo.InvariantCulture); //Gör om DD strängen till en Double
                    break;
                    //return DDval * CalcPts(first, second, third, fourth, fifth);
                }

            }

            finalscore = DDval * CalcPts(first, second, third, fourth, fifth);
            Console.WriteLine(finalscore);
            return finalscore;
        }

        Random random = new Random();

        public double CalcFailiure(double DDinput)
        {
            
            double DDrate = 0;
            DDrate = DDinput;
            int failiureRate = 0;
            int failMargin = 0;
            int successMargin = 0;

            failiureRate = random.Next(0, 101);
            failMargin = random.Next(0, 50);
            successMargin = random.Next(50, 101);

            if (DDrate >= 1 && DDrate <= 1.60)
            {
                if (failiureRate >= 0 && failiureRate <= 5)
                {

                    Console.WriteLine("The jump failed\n");
                    Console.WriteLine("Failiure rate: " + failiureRate + "\n");
                    Console.WriteLine("Fail margin: " + failMargin + "\n");
                    return DDrate;
                }
                else
                {
                    Console.WriteLine("The jump was a success\n");
                    Console.WriteLine("Success rate: " + failiureRate + "\n");
                    Console.WriteLine("Success margin: " + successMargin + "\n");
                    return DDrate;
                }

            }

            return DDrate;
        }


    }
}
